package com.itheima.mp.time;

public class RoundingExample2 {
    public static void main(String[] args) {
        // 测试代码
        int number = 4449;


        int roundedNumber = roundNumber(number);
        System.out.println("Original number: " + number);
        System.out.println("Rounded number: " + roundedNumber);
    }

    public static int roundNumber(int number) {
        int count = 0;
        int number2 = Math.abs(number); // 确保number是正数
        while (number2 > 0) {
            number2 /= 10;
            count++;
        }

        int n=count;

        // 计算n-1位的值
        int power = 1;
        for (int i = 0; i < n - 1; i++) {
            power *= 10;
        }
        int n1Digit = number / power % 10; // 获取n-1位的数字

        // 根据n-1位的数字决定是否进位
        if (n1Digit >= 5) {
            return (number / power + 1) * power; // 进位
        } else {
            return number - (number % power); // 不进位，去掉n-1位及之后的所有数字
        }
    }
}